#define _CRT_SECURE_NO_WARNINGS 1
//#include <fstream>
//#include <iostream>
//#include <string>
//#include <sstream>
//#include<vector>
//#include<algorithm>
//using namespace std;
#include"Stack.h"
void StackInit(Stack* ps)
{
	assert(ps);
	ps->capacity = 0;
	ps->top = -1;
	ps->val = NULL;
}

void StackPush(Stack* ps, STDataType data)
{
	if (ps->top == ps->capacity - 1 || ps->top == -1)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->val,sizeof(STDataType) * newcapacity);

		if (tmp == NULL)
		{
			perror("malloc fail");
		}
		ps->val = tmp;
		ps->capacity = newcapacity;
	}
	
	ps->top++;
	ps->val[ps->top] = data;
}

void StackPop(Stack* ps)
{
	assert(ps);
	ps->top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	return ps->val[ps->top];
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top + 1;
}

int StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == -1;
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	ps->capacity = 0;
	ps->top = -1;
	free(ps->val);
	ps->val = NULL;
}